using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Model;
using SEO.DBSqlHelp;

namespace SEO.DAL
{

    /// <summary>
    /// 版权所有: 版权所有(C) 2011，上海云里网络科技有限公司
    /// 内容摘要: EDMUsersDAL
    /// 完成日期：2011年7月29日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class EDMUsersDAL
    {

        /// <summary>
        /// 获取EDMUsers表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<EDMUsers> GetAllEDMUsersDAL()
        {
            List<EDMUsers> list = new List<EDMUsers>();
            string strSql = "select * from EDMUsers";
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMUsers es = new EDMUsers();
                    es.UserId = int.Parse(dr["user_id"].ToString());
                    es.UserName = dr["user_name"].ToString();
                    es.UserPassword = dr["user_password"].ToString();
                    es.StartDate = dr["start_date"].ToString();
                    es.DayNum = int.Parse(dr["day_num"].ToString());

                    list.Add(es);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取EDMUsers表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<EDMUsers> GetEDMUsersByWhereDAL(string strSql)
        {
            List<EDMUsers> list = new List<EDMUsers>();
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMUsers es = new EDMUsers();
                    es.UserId = int.Parse(dr["user_id"].ToString());
                    es.UserName = dr["user_name"].ToString();
                    es.UserPassword = dr["user_password"].ToString();
                    es.StartDate = dr["start_date"].ToString();
                    es.DayNum = int.Parse(dr["day_num"].ToString());

                    list.Add(es);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往EDMUsers表中插入数据
        /// <param name="es">实例化EDMUsers类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertEDMUsersDAL(SEO.Model.EDMUsers es)
        {
            int iNum = 0;
            string strSql = "insert into EDMUsers(user_name,user_password,start_date,day_num) " +
             " values('" + es.UserName + "','" + es.UserPassword + "','" + es.StartDate + "'," + es.DayNum + ")";
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除EDMUsers表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteEDMUsersDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新EDMUsers表中数据
        /// </summary>
        /// <param name="es">实例化EDMUsers类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateEDMUsersDAL(SEO.Model.EDMUsers es)
        {
            int iNum = 0;
            string strSql = "update EDMUsers set user_name='" + es.UserName + "',user_password='" + es.UserPassword + "',start_date='" + es.StartDate + "',day_num=" + es.DayNum + " where user_id=" + es.UserId;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表EDMUsers到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindEDMUsersDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "EDMUsers");
                //ds.Tables["表名"].Columns.Add("字段名", typeof(string));
                for (int i = 0; i < ds.Tables["EDMUsers"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["EDMUsers"].Rows[i];
                    //dr["字段名"] = "字段值";
                }
                dgv.DataSource = ds.Tables["EDMUsers"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                string strSql = "select top 1 user_id from EDMUsers order by user_id desc";
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["user_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMUsers -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strUserName">用户</param>
        /// <returns>返回 True or False</returns>
        public int CheckUsersIsExist(string strUserName)
        {
            int iNum = 0;
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                string strSql = "select user_id from EDMUsers where user_name='" + strUserName + "'";
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["user_id"].ToString());
                }
                else
                {
                    iNum = 0;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return iNum;
        }

    }
}
